//用户数据模型
var userManage = function() {

    var _init = function() {

        var table = $('#table_user');

        var tableUnit=table.dataTable({
            "processing": true,
            // 开启服务器模式
            serverSide: true,
            "ajax": {
            	url:url+"s",
//            	url:"/a/biz/products",
            },
            //  id, openId, name, gender, dateOfBirth, mobile, nickName, avatarUrl, integral, createDate, 
            //updateDate, createOper, updateOper, balance, wxCode, isMember, source, status, tenantId
            "autoWidth": true,
            "columns": [
            	
            	{
                    "width": "20%",//获取用户头像 
                    "data": "id",
                    "render": function(data, type, row, meta) {
                    	var val="--";
                    	if(data!=null&&data!=''){

            				$.ajax({
            					url : '/a/user/mng/imgurl',
            					type : 'post',
            					dataType : 'json',
            					contentType:"application/json; charset=utf-8",
            					async: false,
            					data : JSON.stringify({ id: data }),
            				}).done(function(r) {
            					if (r.status === '1') { 
            						if(null==r.data.nickName){
            							r.data.nickName="";
            						}
            						val="<img  width='32px'  src='"+r.data.avatarUrl+"' alt='' /><span>"+r.data.nickName+"</span>";//r.data.属性
            					} else if (r.status == '2') {
            					
            					}
            				});
            				return val;
                    	}else
                    		return "--";
                       
                    }
                },{
                    
                    "data": "mobile"
                }/*,
                {
                    "width": "8%",
                    "data": "isMember",
                    "render": function(data, type, row, meta) {
                    	if(data=='0'){
            				return "非会员";
                    }else{
                    	return "会员";
                    }
                    }
                },*/
              /*  {
                    "width": "8%",
                    "data": "integral"
                },*/
                ,{
                    
                    "data": "balance",
                    "render":function(data, type, row, meta) {
                    	if(data){
                    		return data/100;
                    	}
                    	return '0.00';
                    }
                },
                
                {
                    
                    "data": "id" , //消费次数
                    "render": function(data, type, row, meta) {
                    	var val="--";
                    	if(data!=null&&data!=''){

            				$.ajax({
            					url : '/a/user/mng/count',
            					type : 'post',
            					dataType : 'json',
            					contentType:"application/json; charset=utf-8",
            					async: false,
            					data : JSON.stringify({ id: data }),
            				}).done(function(r) {
            					if (r.status === '1') {
            						if(r.data.count!=0)
            						val=r.data.count;//r.data.属性

            					} else if (r.status === '2') {
            						val="--";
            					}
            				});
            				return val;
                    	}else
                    		return "--";	
                    }
                    
                    
                    
                   
                },
                {
                   
                    "data": "id", //消费总额
                    "render": function(data, type, row, meta) {
                    	var val="--";
                    	if(data!=null&&data!=''){

            				$.ajax({
            					url : '/a/user/mng/total',
            					type : 'post',
            					dataType : 'json',
            					contentType:"application/json; charset=utf-8",
            					async: false,
            					data : JSON.stringify({ id: data }),
            				}).done(function(r) {
            					if (r.status === '1') {
            						if(r.data.total!=null)
            						val=r.data.total/100;

            					} else if (r.status === '2') {
            						val="--"
            					}
            				});
            				return val;
                    	}else
                    		return "--";	
                    }
                },
                {
                    
                    "data": "source",
                    "render": function(data, type, row, meta) {
                    	if(data=='1'){
            				return "外部导入";
                    }else if(data=='2'){
                    	return "手工录入";
                    }else{
                    	return "小程序下单";
                    }
                    	}
                },
                {
                    
                    "data": "createDate",
                    "render": function(data, type, row, meta) {
                        return moment(data).format('YYYY-MM-DD HH:mm:ss');
                    }
                },
                {
                   
                    "data": "null",
                    "defaultContent": "<button type=\"button\" class=\"btn blue btn_see_user\">查看</button><button type=\"button\" class=\"btn red btn_edit_user\">编辑</button>"
                }
            ],
            "language": {
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "<div style='width:100%;text-align:center;'>暂无数据记录</div>",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后..."
            },
            "pageLength": 10,
            "order": [
                [6, "desc"]
            ],
            "columnDefs": [{
                "orderable": false,
                "targets": [0,3,4,7]
            }/* ,{
                "targets": 2,
                "render": function(data, type, row, meta) {
                	var val=data;
                	if(data!=null&&data!=''){
        				$.ajax({
        					url : '/a/biz/alevel/obj',
        					type : 'post',
        					dataType : 'json',
        					contentType:"application/json; charset=utf-8",
        					async: false,
        					data : JSON.stringify({ id: data }),
        				}).done(function(r) {
        					if (r.status === '1') {
        						val=r.data.name;

        					} else if (r.status === '2') {
        					}
        				});
        				return val;
                	}else
                		return "";	
                }
            }, {
                "targets": 6,
                "render": function(data, type, row, meta) {
                	if(data=='0'){
                		return "停用";	
                	}else{
                		return "正常";	
                	}
                }
            } ,{
                "targets": 7,
                "render": function(data, type, row, meta) {
                	var val=data;
                	if(data!=null&&data!=''){

        				$.ajax({
        					url : '/a/sys/operator',
        					type : 'post',
        					dataType : 'json',
        					contentType:"application/json; charset=utf-8",
        					async: false,
        					data : JSON.stringify({ id: data }),
        				}).done(function(r) {
        					if (r.status === '1') {
        						val=r.data.name;

        					} else if (r.status === '2') {
        					}
        				});
        				return val;
                	}else
                		return "";	
                }
            }, {
                "targets": 8,
               
            }*/],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>p>"
        });
        	
     /*   // 关闭弹框
        $(".modal").on('hidden.bs.modal', function(e) {
            $('.modal .alert').css('display', 'none'); // 隐藏提示头
            $('.modal .form-group').removeClass('has-error'); //清除错误信息
            $('.modal input[type="text"]').val(''); // 清空 input
            $('.modal input[type="checkbox"]').prop('checked', false); // 清空 input
        });*/
        //获取收款码
      /*  $('#wxdo').click(function(event) {
        	$.ajax({
				url : '/pay/note/recharge',
				type : 'post',
				dataType : 'json',
				contentType:"application/json; charset=utf-8",
				data : JSON.stringify({total_fee:'1'}),
				async: false
			}).done(function(r){
				if (r.status === '1') { 
					$("#wximg").attr("src",r.data);
				} else if (r.status == '2') {
					
				}
			});
        });*/
        $('#btn_new_user').click(function(event) {
        	window.location.href="/p/user/mng/edit?id=null"; 
        });
        //取消
        $('#return_user').click(function(event) {
        	window.history.back();
        });
        // 修改用户
        table.on('click', '.btn_edit_user', function(event) {
            var oData_arr = table.DataTable().rows($(this).parents("tr"))
                .data(); // 操作行对象
            var _id = oData_arr[0].id;
        	window.location.href="/p/user/mng/edit?id="+ encodeURIComponent(_id);
        });
       //时间日历
        $('#brithday').datepicker({  
        	
             language:"zh-CN", 
            todayBtn : "linked",  
            autoclose : true,  
            todayHighlight : true,  
            endDate : new Date()  
        })
        //批量导入
        $("#pl_insert").click(function(){
        	 window.location.href="/p/user/mng/toAdd";
        });
        $(".changfile").click(function(){
            $("#csvFile").trigger('click');
            $("#csvFile").change(function(){
               $("#filename").val($("#csvFile").val());
            });
        });
        //用户动态查询
        $('#doQuery').click(function(event){
  		  //var province =   $("#distpicker1 select[name='province']").val();
  		  //var city =   $("#distpicker1 select[name='city']").val();
  		  var _isMember =   $("#search select[name='isMember']").val();
  		 // var start =   $("#datepicker input[name='start']").val();
  		 // var end =   $("#datepicker input[name='end']").val();
  		  var _source =   $("#search select[name='source']").val();
  		 var _like =   $("#search input[name='like']").val();
  		 // var levelId =   $("#search select[name='']").val();
  		  var param={isMember:_isMember,source:_source,like:_like/*,"name":name,"phone":phone,"levelId":levelId,"createDate":start,"updateDate":end*/};
  		table.DataTable().search(JSON.stringify(param)).draw();
        //table.DataTable().ajax.reload();
  		});
        
        // 查看用户详情
        table.on('click', '.btn_see_user', function(event) {
            var oData_arr = table.DataTable().rows($(this).parents("tr"))
                .data(); // 操作行对象
            var _id = oData_arr[0].id;
            window.location.href="/p/user/mng/user?id="+ encodeURIComponent(_id);
  /*          bootbox.dialog({
                message: "确定要删除",
                title: "提示",
                buttons: {
                    cancel: {
                        label: "取消",
                        className: "btn dark btn-outline"
                    },
                    success: {
                        label: "确定",
                        className: "btn green",
                        callback: function() {
                            // alert('提交删除');
                            var _data = JSON.stringify({ id: _id });
                            request_role_del(_data, function(res) {
                                // 正确
                                table.DataTable().ajax.reload();
                            }, function(err) {
                                //	错误
                                alert(err);
                            })
                        }
                    }
                }
            });*/
        });
    /*    // 重置密码
        table.on('click', '.btn_repwd_role', function(event) {
            var oData_arr = table.DataTable().rows($(this).parents("tr"))
                .data(); // 操作行对象
            var _id = oData_arr[0].id;
            bootbox.dialog({
                message: "确定要重置密码吗？(默认重置为123456)",
                title: "提示",
                buttons: {
                    cancel: {
                        label: "取消",
                        className: "btn dark btn-outline"
                    },
                    success: {
                        label: "确定",
                        className: "btn green",
                        callback: function() {
                        	
							$.ajax({
								 url: '/a/biz/agentpwd',
								 type: 'put',
								 dataType: 'json',
								 contentType:"application/json; charset=utf-8",
								 data: JSON.stringify({
										id : _id
									}),
								 })
								 .done(function(data) {
									 table.DataTable().ajax.reload();
								 });
                        }
                    }
                }
            });
        });*/
    }

    return {
        init: function() {
            _init();
        }

    };

}();


//消费记录
var orderManage = function() {

    var _init = function() {

        var table = $('#table_order');
        var id=$("#adress_user_id").val();
        var tableUnit=table.dataTable({
            "processing": true,
            // 开启服务器模式
            serverSide: true,
            "ajax": {
            	url:"/a/user/mng/order",
            	type:"get",
            	data:{"id":id},
            	dataType : 'json',
				contentType:"application/json; charset=utf-8"
            },
            "autoWidth": true,
            "columns": [
            	{
                    "width": "15%",
                    "data":"orderNO"  
                },
            	{
                    "width": "10%",
                    "data": "id",
                    "render": function(data, type, row, meta) {
						var val=0;
						if(data!=null&&data!=''){

							$.ajax({
								url : '/a/user/order/product',
								type : 'post',
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
								async: false,
								data : JSON.stringify({ id: data }),
							}).done(function(r) {
								if (r.status === '1') { 
									if(r.data.length>=1){
										for(var i=0;i<r.data.length;i++){
											
											val+=r.data[i].count*1;// r.data.属性
										}
									}else{
										val="--";// r.data.属性
									}
								} else if (r.status == '2') {

								}
							});
							return val;
						}else{
							
						
							return "--";
						}

					}
                },{
                    "width": "10%",
                    "data":"orderMoney",
					"render":function(data, type, row, meta) {
						if('0'==data){
							return data;
						}else{
							return data/100;
						}
					}
                },
                {
                    "width": "10%",
                    "data":"saleMoney",
					"render":function(data, type, row, meta) {
						if('0'==data){
							return data;
						}else{
							return data/100;
						}
					}
                },
                {
                    "width": "10%",
                    "data": "total",
					"render":function(data, type, row, meta) {
						var val="--";
						if(row.linkType == '2'){
							
						}else{
							val= data/100;
						}
						return val;
					}
                },
                {
                    "width": "10%",
                    "data": "integralPrice",
					"render":function(data, type, row, meta) {
						var val="--";
						if(row.linkType == '2'){
							val=data;
						}
						return val;
					}
                }
                ,
				{
					"width": "15%",
					"data": "orderDate",
					"render":function(data, type, row, meta) {
						return moment(data).format('YYYY-MM-DD HH:mm:ss');
					}


				},
                {
					"width": "10%",
					"data": "status",
					"render": function(data, type, row, meta) {
						// 到店和不到店
						var val="--";
								switch (data) {
								case 2:
									val="<button type=\"button\" onclick=\"shipments($(this))\" class=\"btn  blue \">发货</button>";
									break;
								case 7:
									val="<button type=\"button\" class=\"btn  green btn_see_order1\">详情</button>";
									break;
								case 8:
									val="<button type=\"button\" class=\"btn  green btn_see_order1\">详情</button>";
									break;
								default:
									val="<button type=\"button\" class=\"btn  green btn_see_order\">详情</button>";
								break;
								}

							
						

						return val;
					}
				}

            ] ,  "pageLength": 5,
			"order": [
				
				],"columnDefs" : [ {
                "orderable":false,//禁用排序
                 "targets":[0,1,2,3,4,5,6,7]   //指定的列
             }
            ],
            "language": {
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "暂无数据记录",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后..."
            },
     
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>p>"
          
        });
    	// 查看订单详情
		table.on('click', '.btn_see_order', function(event) {
			var oData_arr = table.DataTable().rows($(this).parents("tr"))
			.data(); // 操作行对象
			var _id = oData_arr[0].id;
			window.location.href="/p/user/order/view?id="+ encodeURIComponent(_id);
		});
		// 查看到店订单详情
		table.on('click', '.btn_see_order1', function(event) {
			var oData_arr = table.DataTable().rows($(this).parents("tr"))
			.data(); // 操作行对象
			var _id = oData_arr[0].id;
			window.location.href="/p/user/storeOrder/view1?id="+ encodeURIComponent(_id);
		});   
    }
    return {
        init: function() {
            _init();
        }
    };
}();
//售后记录
var refundManage = function() {
    var _init = function() {
        var table = $('#table_refund');
        var id=$("#adress_user_id").val();
        var tableUnit=table.dataTable({
            "processing": true,
            // 开启服务器模式
            serverSide: true,
            "ajax": {
            	url:"/a/user/mng/refund",
            	type:"get",
            	data:{"id":id},
            	dataType : 'json',
				contentType:"application/json; charset=utf-8"
            },
            "autoWidth": false,
            "columns": [
            	{
            		"width": "40%",// 商品
					"data":"orderProductDetailId",
					"render": function(data, type, row, meta) {
						var val="";
						if(data!=null&&data!=''){
							$.ajax({
								url : '/a/user/order/product1',
								type : 'post',
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
								async: false,
								data : JSON.stringify({ orderProductDetailId: data }),
							}).done(function(r) {
								if (r.status === '1') {
									
									if(r.data != null){
										val+=r.data.productName;// r.data.属性
									}else{
										val="--";// r.data.属性
									}
								} else if (r.status == '2') {

								}
							});
							return val;
						}else
							return "--";
					}
                },
            	{
                	"width": "10%",// 商品
					"data":"orderProductDetailId",
					"render": function(data, type, row, meta) {
						var val="";
						if(data!=null&&data!=''){
							$.ajax({
								url : '/a/user/order/product1',
								type : 'post',
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
								async: false,
								data : JSON.stringify({ orderProductDetailId: data }),
							}).done(function(r) {
								if (r.status === '1') {
									
									if(r.data != null){
										val+=r.data.count;// r.data.属性
									}else{
										val="--";// r.data.属性
									}
								} else if (r.status == '2') {

								}
							});
							return val;
						}else
							return "--";

					}
                },{
                	"width": "10%",// 订单金额
					"data":"applicationBackId",
					"render":function(data, type, row, meta) {
						var val="--";
						if(data!=null&&data!=''){
							$.ajax({
								url : '/a/user/refundOrder/order',
								type : 'post',
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
								async: false,
								data : JSON.stringify({ id: data }),
							}).done(function(r) {
										val=r.data;
							});
							
						}
						return val;
					}
                },
                {
                	"width": "10%",// 订单金额
					"data":"applicationBackId",
					"render":function(data, type, row, meta){
						var val="--";
						if(data!=null&&data!=''){
							$.ajax({
								url : '/a/user/refundOrder/appback',
								type : 'post',
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
								async: false,
								data : JSON.stringify({ id: data }),
							}).done(function(r) {
										val=r.data/100;
							});
							
						}
						return val;
					}
                },
                {
                	"width": "10%",// 订单金额
					"data":"applicationBackId",
					"render":function(data, type, row, meta){
						var val="--";
						if(data!=null&&data!=''){
							$.ajax({
								url : '/a/user/refundOrder/appback1',
								type : 'post',
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
								async: false,
								data : JSON.stringify({ id: data }),
							}).done(function(r) {
										val=r.data;
							});
							
						}
						return val;
					}
                },
                {
                	"width": "10%",
					"data": "createDate",
					"render":function(data, type, row, meta) {
						return moment(data).format('YYYY-MM-DD HH:mm:ss');
					}
                }
                ,
                {
                	"width": "10%",
					"data": "status",
					"render": function(data, type, row, meta) {
						// 到店和不到店
						var val="--";
								switch (data) {
								case 4:
									val="<button type=\"button\" onclick=\"refund($(this))\" data-toggle=\"modal\" data-target=\"#myModal2\"  class=\"btn  green btn_refund_order\">退款</button>";
									break;
								case 1:
									val="<button type=\"button\" onclick=\"endRefund($(this))\" data-toggle=\"modal\" data-target=\"#myModal3\" class=\"btn  green btn_do_order\">详情</button>";
									break;
								case 2:
									val="<button type=\"button\" onclick=\"endRefund($(this))\" data-toggle=\"modal\" data-target=\"#myModal3\" class=\"btn  green btn_end_order\">完成</button>";
									break;
								}
						return val;
					}
				}

            ],  "pageLength": 5,
			"order": [
				
				],"columnDefs" : [ {
                "orderable":false,//禁用排序
                 "targets":[0,1,2,3,4,5,6]   //指定的列
             }
            ],
            "language": {
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "暂无数据记录",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后..."
            },
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>p>"
        });
		$("#yesOrder").click(function(){
			//ajax处理  url  /a/user/refundOrder/doRefund
			var _data = $("#refund").serializeObject();
			var _id=_data.id;
			if(_data.status == 1){
				if (!mdTool.validate(_data.content, 'require')) {
					mdTool.showMessage1('请输入不同意原因');
					return;
				}
				else {
					mdTool.showMessage1('');
					$.ajax({
						url : '/a/user/refundOrder/doRefund',
						type : 'post',
						dataType : 'json',
						contentType:"application/json; charset=utf-8",
						async: false,
						data : JSON.stringify({ id: _id,status:_data.status,content:_data.content}),
					}).done(function(r) {
						if (r.status === '1') { 
							alert("处理成功");
							$('#myModal2').modal('hide');
							$("#table_refund").DataTable().ajax.reload(); // 刷新table
						} else if (r.status === '2') {
							alert(r.message);
							$('#myModal2').modal('hide');
						}
					});
				}
			}else{
				if (!mdTool.validate(_data.total, 'require')) {
					mdTool.showMessage1('请输入数额');
					return;
				}
				if (!mdTool.validate(_data.total, 'two')) {
					mdTool.showMessage1('请输入正确的数额');
					return;
				}
				else {
					mdTool.showMessage1('');
					$.ajax({
						url : '/a/user/refundOrder/doRefund',
						type : 'post',
						dataType : 'json',
						contentType:"application/json; charset=utf-8",
						async: false,
						data : JSON.stringify({ id: _id,status:_data.status, total: _data.total}),
					}).done(function(r) {
						if (r.status === '1') { 
							alert("处理成功");
							$('#myModal2').modal('hide');
							$("#table_refund").DataTable().ajax.reload(); // 刷新table
						} else if (r.status === '2') {
							alert(r.message);
							$('#myModal2').modal('hide');
						}
					});
				}
			}
		})
			
		/**
		 * 留言展示
		 */
		$(".radios").click(function(){
			var val=$(this).val();
			if(val=="1")
			{
				$("#money").hide();
				$("#liuyan").show();
			}
			else 
			{
				$("#liuyan").hide();
				$("#money").show();
			}
		})
    }

    return {
        init: function() {
            _init();
        }

    };

}();

//积分明细
var intManage = function() {
    var _init = function() {
        var table = $('#table_int');
        var id=$("#adress_user_id").val();
        var tableUnit=table.dataTable({
            "processing": true,
            // 开启服务器模式
            serverSide: true,
            "ajax": {
            	url:"/a/user/mng/int",
            	type:"get",
            	data:{"id":id},
            	dataType : 'json',
				contentType:"application/json; charset=utf-8"
            },
            "autoWidth": true,
            "columns": [
            	{
            		"width": "8%",// 商品
					"data":"instruction"
                },{
            		"width": "8%",// 商品
					"data":"type",
					"render":function(data, type, row, meta) {
						if(data == 1){
							return "消耗";
						}else{
							return "获取";
						}
					}
                },
                
            	{
                	"width": "8%",// 商品
					"data":"integral"
                },
                {
                	"width": "8%",
					"data": "createDate",
					"render":function(data, type, row, meta) {
						return moment(data).format('YYYY-MM-DD HH:mm:ss');
					}
                }

            ],  "pageLength": 5,
			"order": [
				
				],  "columnDefs" : [ {
                "orderable":false,//禁用排序
                 "targets":[0,1,2]//指定的列
             }
            ],
            "language": {
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "暂无数据记录",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后..."
            },
        
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>p>"
        });
    }
    return {
        init: function() {
            _init();
        }

    };

}();


//返利收益
var commManage = function() {
    var _init = function() {
        var table = $('#table_comm');
        var id=$("#adress_user_id").val();
        var tableUnit=table.dataTable({
            "processing": true,
            // 开启服务器模式
            serverSide: true,
            "ajax": {
            	url:"/a/user/mng/comm",
            	type:"get",
            	data:{"id":id},
            	dataType : 'json',
				contentType:"application/json; charset=utf-8"
            },
            "autoWidth": true,
            "columns": [
       /*     	{
            		"width": "8%",// 商品
					"data":"userId",
					"render" : function(data, type, row, meta){
						if(data!=null&&data!=''){
							$.ajax({
								url : '/a/user/getUser',
								type : 'post',
								data : JSON.stringify({ userId: data}),
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
							}).done(function(r) {
								data=r.data;
							});
							
						}
						return data;
					}
                },*/
            	{
                	"width": "8%",// 商品
					"data":"merchantOrderNo"
                },
                {
                	"width": "8%",
					"data": "orderId",
					"render":function(data, type, row, meta){
						var val="--";
						if(data!=null&&data!=''){
							$.ajax({
								url : '/a/user/order/orderOne',
								type : 'post',
								dataType : 'json',
								contentType:"application/json; charset=utf-8",
								async: false,
								data : JSON.stringify({ id: data }),
							}).done(function(r) {
										val=r.data/100;
							});
						}
						return val;
					}
                }
                ,  {
                	"width": "8%",
					"data": "createDate",
					"render":function(data, type, row, meta) {
						return moment(data).format('YYYY-MM-DD HH:mm:ss');
					}
                }, {
                	"width": "8%",
					"data": "amount",
					"render":function(data, type, row, meta) {
						return data/100;
					}
                }

            ],  "pageLength": 5,
			"order": [
				
				],  "columnDefs" : [ {
                "orderable":false,//禁用排序
                 "targets":[0,1,2,3]//指定的列
             }
            ],
            "language": {
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "暂无数据记录",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后..."
            },
        
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>p>"
        });
    }
    return {
        init: function() {
            _init();
        }

    };

}();

//提现管理
var tixianManage = function() {
    var _init = function() {
        var table = $('#table_tixian');
        var id=$("#adress_user_id").val();
        var tableUnit=table.dataTable({
            "processing": true,
            // 开启服务器模式
            serverSide: true,
            "ajax": {
            	url:"/a/user/mng/tixian",
            	type:"get",
            	data:{"id":id},
            	dataType : 'json',
				contentType:"application/json; charset=utf-8"
            },
            "autoWidth": true,
            "columns": [
            	{
                	"width": "8%",
					"data": "createDate",
					"render":function(data, type, row, meta) {
						return moment(data).format('YYYY-MM-DD HH:mm:ss');
					}
                },
                {
                	"width": "8%",
					"data": "amount",
					"render":function(data, type, row, meta) {
						return data/100;
					}
                },
            	{
                	"width": "8%",// 商品
                	"data" : "null",
					"defaultContent" : "微信零钱"
                },
                {
                	"width": "8%",
					"data": "tradeNo"
                },{
                	"width": "8%",
					"data": "tradeDate",
					"render":function(data, type, row, meta) {
						return moment(data).format('YYYY-MM-DD HH:mm:ss');
					}
                }

            ],  "pageLength": 5,
			"order": [
				
				],  "columnDefs" : [ {
                "orderable":false,//禁用排序
                 "targets":[0,1,2,3,4]//指定的列
             }
            ],
            "language": {
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "暂无数据记录",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后..."
            },
        
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'table_b relative'<'col-md-5'i><'col-md-7'>p>"
        });
    }
    return {
        init: function() {
            _init();
        }

    };

}();


//用户信息页跳转编辑
$("#user_jbzl_eidt").click(function(){
	
	var _id=$("#adress_user_id").val();
	window.location.href="/p/user/mng/edit?id="+ encodeURIComponent(_id);
});
// 确定添加，添加用户数据校验
//$("#form_role_new").find('button[type=submit]').click(function() {
	$("#sava_user").click(function() {
    var _data = $("#form_user_new").serializeObject();
    var _id=_data.id;
	var param={"user":{"id":_id,"nickName":_data.nickName,"mobile":_data.mobile,"name":_data.name,"gender":_data.gender,"dateOfBirth":_data.dateOfBirth,
		"isMember":_data.isMember}}
	if (!mdTool.validate(_data.nickName, 'require')) {
		mdTool.showMessage1('请输入昵称');
		return;
	}
	if (!mdTool.validate(_data.mobile, 'require')) {
		mdTool.showMessage1('请重新输入手机号');
		return;
	}
	if (!mdTool.validate(_data.mobile, 'phone')) {
		mdTool.showMessage1('手机号码格式不正确，请重新输入');
		return;
	}
	if (!mdTool.validate(_data.name, 'require')) {
		mdTool.showMessage1('请输入你的姓名');
		return;
	}
	if (!mdTool.validate(_data.gender, 'require')) {
		mdTool.showMessage1('请选择一个性别');
		return;
	}
	if (!mdTool.validate(_data.dateOfBirth, 'require')) {
		mdTool.showMessage1('请输入生日');
		return;
	}
	else {
				// 正确	
				var _type = _id == null||_id == '' ? "put" : "post";

				$.ajax({
					url : '/a/user/mng/edit',
					type : _type,
					dataType : 'json',
					contentType:"application/json; charset=utf-8",
					data : JSON.stringify(param),
				}).done(function(data) {
					if (data.status === '1') {
						// 正确
			        	window.location.href="/p/user/mng";

					} else if (data.status === '2') {
//						错误
						mdTool.showMessage1(data.message);
					}
				});
}});
	//批量导出
	$("#pl_toin").click(function(){
		window.location.href='/a/user/mng/exportUser';
	})
		//模板下载
	$("#pl_toin1").click(function(){
		window.location.href='/a/user/mng/exportUser1';
	})
  //批量导入数据校验
	$("#sava_users").click(function() {
	    var _data = $("#filename").val();
		if (!mdTool.validate(_data, 'require')) {
			mdTool.showMessage1('请选择一个文件');
			return;
		}if (_data.indexOf('.csv')<0) {
			mdTool.showMessage1('请选择.csv格式文件');
			return;
		}
		else {
			mdTool.showMessage1('');
			$("#form_user_add").submit();	
	}});

$(function() {
    userManage.init();
    orderManage.init();
    refundManage.init();
    intManage.init();
    commManage.init();
    tixianManage.init();
});
//发货
function shipments(dom){
	var oData_arr = $('#table_order').DataTable().rows(dom.parents("tr")).data(); // 操作行对象
	var _id = oData_arr[0].id;
	window.location.href="/p/user/order/shipments?id="+ encodeURIComponent(_id);
};
//退款
function refund(dom){
	var oData_arr = $('#table_refund').DataTable().rows(dom.parents("tr")).data(); // 操作行对象
	var _id = oData_arr[0].id;
	$.ajax({
		url : '/a/user/refundOrder/order2',
		type : 'post',
		dataType : 'json',
		contentType:"application/json; charset=utf-8",
		async: false,
		data : JSON.stringify({ id: _id }),
	}).done(function(r) {
		$("#mytag").val(r.data[0][0]);
		var html="";
		if(r.data[1].length==3){
			html="<div class=\"col-xs-3 text-center\">售后凭证</div><div class=\"col-xs-8\"><span>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][0]+"\"><img src=\""+r.data[1][0]+"\"   width=\"81px\" height=\"81px\"/></a>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][1]+"\"><img src=\""+r.data[1][1]+"\"  style=\"margin-left: 3%\" width=\"81px\" height=\"81px\"/></a>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][2]+"\"><img src=\""+r.data[1][2]+"\"  style=\"margin-left: 3%\" width=\"81px\" height=\"81px\"/></a>" +
			"</span></div>";
		}
		if(r.data[1].length==2){
			html="<div class=\"col-xs-3 text-center\">售后凭证</div><div class=\"col-xs-8\"><span>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][0]+"\"><img src=\"$"+r.data[1][0]+"\"   width=\"81px\" height=\"81px\"/></a>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][1]+"\"><img src=\"$"+r.data[1][1]+"\"  style=\"margin-left: 3%\" width=\"81px\" height=\"81px\"/></a>" +
			"</span></div>";
		}
		if(r.data[1].length==1){
			html="<div class=\"col-xs-3 text-center\">售后凭证</div><div class=\"col-xs-8\"><span>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][2]+"\"><img src=\"$"+r.data[1][2]+"\"   width=\"81px\" height=\"81px\"/></a>" +
			"</span></div>";
		}
		$("#shDetail").html();
		$("#shDetail").html(
				"<div class=\"col-xs-3 text-center\">申请时间</div><div class=\"col-xs-8\">"+r.data[0][1]+"</div>" +
				"<div class=\"col-xs-12\" style=\"height:10px\"></div><div class=\"col-xs-3 text-center\">商品状态</div>"+
				"<div class=\"col-xs-8\">"+r.data[0][2]+"</div><div class=\"col-xs-12\" style=\"height:10px\"></div>" +
				"<div class=\"col-xs-3 text-center\">售后原因</div><div class=\"col-xs-8\">"+r.data[0][3]+"</div>" +
				"<div class=\"col-xs-12\" style=\"height:10px\"></div><div class=\"col-xs-3 text-center\">"+r.data[0][4]+"</div>" +
				"<div class=\"col-xs-8\" style=\"color:red\">"+r.data[0][5]+"</div><div class=\"col-xs-12\" style=\"height:10px\"></div>" +html
		);
	});
};
// 退款完成
function endRefund(dom){
	var oData_arr = $('#table_refund').DataTable().rows(dom.parents("tr")).data(); // 操作行对象
	var _id = oData_arr[0].id;
	$.ajax({
		url : '/a/user/refundOrder/order2',
		type : 'post',
		dataType : 'json',
		contentType:"application/json; charset=utf-8",
		async: false,
		data : JSON.stringify({ id: _id }),
	}).done(function(r) {
		var html="";
		var tem="<div class=\"col-xs-12\" style=\"height:10px\"></div><div class=\"col-xs-3 text-center\">处理结果</div><div class=\"col-xs-8\">" +
		r.data[0][6]+"</div><div class=\"col-xs-12\" style=\"height:10px\"></div><div class=\"col-xs-3 text-center\">处理时间</div>" +
		"<div class=\"col-xs-8\">"+r.data[0][7]+"</div>";
		
		
		
		if(r.data[1].length==3){
			html="<div class=\"col-xs-3 text-center\">售后凭证</div><div class=\"col-xs-8\"><span>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][0]+"\"><img src=\""+r.data[1][0]+"\"   width=\"81px\" height=\"81px\"/></a>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][1]+"\"><img src=\""+r.data[1][1]+"\"  style=\"margin-left: 3%\" width=\"81px\" height=\"81px\"/></a>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][2]+"\"><img src=\""+r.data[1][2]+"\"  style=\"margin-left: 3%\" width=\"81px\" height=\"81px\"/></a>" +
			"</span></div>";
		}
		if(r.data[1].length==2){
			html="<div class=\"col-xs-3 text-center\">售后凭证</div><div class=\"col-xs-8\"><span>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][0]+"\"><img src=\"$"+r.data[1][0]+"\"   width=\"81px\" height=\"81px\"/></a>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][1]+"\"><img src=\"$"+r.data[1][1]+"\"  style=\"margin-left: 3%\" width=\"81px\" height=\"81px\"/></a>" +
			"</span></div>";
		}
		if(r.data[1].length==1){
			html="<div class=\"col-xs-3 text-center\">售后凭证</div><div class=\"col-xs-8\"><span>" +
			"<a rel=\"example_group\" target=\"_blank\" href=\""+r.data[1][2]+"\"><img src=\"$"+r.data[1][2]+"\"   width=\"81px\" height=\"81px\"/></a>" +
			"</span></div>";
		}
		$("#shDetail1").html();
		$("#shDetail1").html(
				"<div class=\"col-xs-3 text-center\">申请时间</div><div class=\"col-xs-8\">"+r.data[0][1]+"</div>" +
				"<div class=\"col-xs-12\" style=\"height:10px\"></div><div class=\"col-xs-3 text-center\">商品状态</div>"+
				"<div class=\"col-xs-8\">"+r.data[0][2]+"</div><div class=\"col-xs-12\" style=\"height:10px\"></div>" +
				"<div class=\"col-xs-3 text-center\">售后原因</div><div class=\"col-xs-8\">"+r.data[0][3]+"</div>" +
				"<div class=\"col-xs-12\" style=\"height:10px\"></div><div class=\"col-xs-3 text-center\">"+r.data[0][4]+"</div>" +
				"<div class=\"col-xs-8\" style=\"color:red\">"+r.data[0][5]+"</div><div class=\"col-xs-12\" style=\"height:10px\"></div>" +html+tem
		);
	});
};
/*function tojifen(){
	var id=$('#adress_user_id').val();
	window.location.href="/p/wxconf/comm/jifen?uperUserId="+encodeURIComponent(id);
}
function toyongjin(){
	var id=$('#adress_user_id').val();
	window.location.href="/p/wxconf/comm/profit?uperUserId="+encodeURIComponent(id);
}
function totixian(){
	var id=$('#adress_user_id').val();
	window.location.href="/p/wxconf/comm/commission?uperUserId="+encodeURIComponent(id);
}*/